import table from '@/components/dt/table/render.js'
import Opt from '@/components/dt/info/Opt.js'
import tool from '@/common/tool'
let _this = null
let tmpInfo = {}
const clueHead = [
  {
    field: 'target1',
    title: '顾问线索目标(组)',
    minWidth: 180,
    slots: {
      default: (params, h) => {
        return table.cell.input(_this, h, {
          key: 'target1',
          type: 'number',
          data: params.row,
          disabled: _this.isCheck,
          blur: (key) => {
            inputChange(key, params.row)
          }
        })
      }
    }
  }
]
const visitorHead = [
  {
    field: 'target1',
    title: '顾问来人目标(组)',
    minWidth: 180,
    slots: {
      default: (params, h) => {
        return table.cell.input(_this, h, {
          key: 'target1',
          type: 'number',
          data: params.row,
          disabled: _this.isCheck,
          blur: (key) => {
            inputChange(key, params.row)
          }
        })
      }
    }
  }
]
const subscriptionHead = [
  {
    field: 'target1',
    title: '认购套数目标(套)',
    minWidth: 150,
    slots: {
      default: (params, h) => {
        return table.cell.input(_this, h, {
          key: 'target1',
          type: 'number',
          data: params.row,
          disabled: _this.isCheck,
          blur: (key) => {
            inputChange(key, params.row)
          }
        })
      }
    }
  },
  {
    field: 'target2',
    title: '认购金额目标(万元)',
    minWidth: 150,
    slots: {
      default: (params, h) => {
        return table.cell.input(_this, h, {
          key: 'target2',
          type: 'number',
          data: params.row,
          disabled: _this.isCheck,
          blur: (key) => {
            inputChange(key, params.row, 2)
          }
        })
      }
    }
  },

  {
    field: 'target3',
    title: '认购面积目标(㎡)',
    minWidth: 150,
    slots: {
      default: (params, h) => {
        return table.cell.input(_this, h, {
          key: 'target3',
          type: 'number',
          data: params.row,
          disabled: _this.isCheck,
          blur: (key) => {
            inputChange(key, params.row, 2)
          }
        })
      }
    }
  }
]
const contractHead = [
  {
    field: 'target1',
    title: '签约套数目标(套)',
    minWidth: 150,
    slots: {
      default: (params, h) => {
        return table.cell.input(_this, h, {
          key: 'target1',
          type: 'number',
          data: params.row,
          disabled: _this.isCheck,
          blur: (key) => {
            inputChange(key, params.row)
          }
        })
      }
    }
  },
  {
    field: 'target2',
    title: '签约金额目标(万元)',
    minWidth: 150,
    slots: {
      default: (params, h) => {
        return table.cell.input(_this, h, {
          key: 'target2',
          type: 'number',
          data: params.row,
          disabled: _this.isCheck,
          blur: (key) => {
            inputChange(key, params.row, 2)
          }
        })
      }
    }
  },
  {
    field: 'target3',
    title: '签约面积目标(㎡)',
    minWidth: 150,
    slots: {
      default: (params, h) => {
        return table.cell.input(_this, h, {
          key: 'target3',
          type: 'number',
          data: params.row,
          disabled: _this.isCheck,
          blur: (key) => {
            inputChange(key, params.row, 2)
          }
        })
      }
    }
  }
]
const collectionHead = [
  {
    field: 'target2',
    title: '顾问回款金额目标(万元)',
    minWidth: 180,
    slots: {
      default: (params, h) => {
        return table.cell.input(_this, h, {
          key: 'target2',
          type: 'number',
          data: params.row,
          disabled: _this.isCheck,
          blur: (key) => {
            inputChange(key, params.row, 2)
          }
        })
      }
    }
  }
]
//输入框改变
function inputChange(key, data, toFixed = 0) {
  if (!data[key]) {
    data[key] = 0
  } else {
    data[key] = Number(data[key]).toFixed(toFixed)
  }
  let list = _this.list

  if (key == 'target1' && (_this.modalType == 'CLUETARGET' || _this.modalType == 'VISITOR')) {
    _this.counselorTotal = sumFun(list, key)
  }
  if (_this.modalType == 'SUBSCRIPTION' || _this.modalType == 'CONTRACT') {
    if (key === 'target1') {
      _this.userTotal.goalCount = sumFun(list, key)
    } else if (key === 'target2') {
      _this.userTotal.goalMoney = sumFun(list, key).toFixed(2)
    } else if (key === 'target3') {
      _this.userTotal.goalArea = sumFun(list, key).toFixed(2)
    }
  }
  if (key == 'target2' && _this.modalType == 'PAYMENT') {
    _this.counselorTotal = sumFun(list, key).toFixed(2)
  }
}

function sumFun(arr, key) {
  let sum = arr.reduce(function (sum, ele) {
    return sum + Number(ele[key] ? ele[key] : 0)
  }, 0)
  return sum ? sum : 0
}

function changeHead(modalType) {
  let tmpArr = []
  switch (modalType) {
    case 'CLUETARGET':
      tmpArr = clueHead
      break
    case 'VISITOR':
      tmpArr = visitorHead
      break
    case 'SUBSCRIPTION':
      tmpArr = subscriptionHead
      break
    case 'CONTRACT':
      tmpArr = contractHead
      break
    case 'PAYMENT':
      tmpArr = collectionHead
      break
    default:
      tmpArr = []
      break
  }
  return tmpArr
}

class column extends Opt {
  constructor(vm) {
    super()
    _this = vm
    this.build()
  }

  build() {
    this.createAll()
    tmpInfo = tool.deepClone(_this.info)
    this.opts.push(...this.all)
  }

  createAll() {
    this.all = [
      {
        field: 'userName',
        title: '顾问姓名',
        minWidth: 130
      },
      {
        field: 'workNo',
        title: '顾问工号',
        minWidth: 130
      },
      {
        field: 'stateStr',
        title: '顾问状态',
        minWidth: 130
      },
      ...changeHead(_this.modalType),
      {
        field: 'handles',
        title: '调整记录',
        width: 100,
        fixed: 'right',
        visible: _this.setGoalType == 'ADJUST',
        cell: (item) => {
          let btn = []
          btn.push({
            title: item.upNum,
            event: 'changeLog'
          })
          return {
            btn
          }
        }
      }
    ]
  }
}

export default column
